package com.timoon.sample.web.product.panel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.ListChoice;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.form.upload.FileUploadField;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.googlecode.wicket.jquery.ui.Options;
import com.googlecode.wicket.jquery.ui.form.button.AjaxButton;
import com.googlecode.wicket.jquery.ui.kendo.editor.Editor;
import com.timoon.app.utils.Constantor;
import com.timoon.component.panel.common.utils.ValidatorFeedbackPanel;
import com.timoon.component.panel.editor.EditorPanel;
import com.timoon.component.panel.jqgrid.JqGridPanel;
import com.timoon.component.panel.jqgrid.JqGridService;
import com.timoon.component.panel.jqgrid.action.SaveButton;
import com.timoon.component.panel.jqgrid.panel.CreatorPanel;
import com.timoon.sample.dao.model.Product;

/**
 *<p></p>
 *<p></p>
 *<p>Copyright:Copyright(c)2013</p>
 *<p>Company:timoon</p>
 *@author 付乐天
 *@version 1.0 */

public class ProductCreatorPanel extends CreatorPanel {

	@SpringBean(name="com.timoon.webcomponent.utility.product.service.ProductService")
	private JqGridService service;
	
	private Product product;
	
	private String recordId;
	
	private JqGridPanel mainPanel;
	
	public ProductCreatorPanel(String id, JqGridPanel mainPanel, String recordId) {
		super(id, mainPanel, recordId);
		// TODO Auto-generated constructor stub
		
		this.recordId=recordId;
		
		this.mainPanel=mainPanel;
	
		renderPanel();
	}

	private void renderPanel() {
		
		//初始化produc
		if(this.recordId==null){
			this.product=new Product();
		}else{
			this.product=(Product) this.service.loadRecordById(recordId);
		}

		
		renderForm();
	}

	private void renderForm() {
		// TODO Auto-generated method stub
		Form form=new Form("wk_form");
		add(form);
		
		renderBasicInfo(form);
	}

	private void renderBasicInfo(Form form) {
		// TODO Auto-generated method stub
		
		form.add(new TextField<>("wk_name",new PropertyModel<>(product, "name")));
		
		TextField marketPrice=new TextField<>("wk_market_price", new PropertyModel<>(product, "marketPrice"));
		form.add(marketPrice);
		
		TextField acturePrice=new TextField<>("wk_actrue_price", new PropertyModel<>(product, "actualPrice"));
		form.add(acturePrice);
		
		TextArea<String> saleInfo=new TextArea<String>("wk_sale_info", new PropertyModel<String>(product, "saleInfo"));
		form.add(saleInfo);
		
		TextArea serviceInfo=new TextArea<>("wk_service_info", new PropertyModel<>(product, "serviceInfo"));
		form.add(serviceInfo);
		
		TextField rate=new TextField<>("wk_rate", new PropertyModel<>(product, "rate"));
		form.add(rate);
		
		TextArea<String> packList=new TextArea<>("wk_pack_list", new PropertyModel<String>(product, "packList"));
		form.add(packList);
		
		TextArea<String> serviceSale=new TextArea<>("wk_service_after_sale", new PropertyModel<String>(product, "serviceAfterSale"));
		form.add(serviceSale);
		
		List<Integer> status=new ArrayList<Integer>();
		for(Entry<Integer, String> entry:Constantor.PRODUCT_STATUS.entrySet()){
			status.add(entry.getKey());
		}
		
		IChoiceRenderer choiceRender=new IChoiceRenderer() {

			@Override
			public Object getDisplayValue(Object arg0) {
				// TODO Auto-generated method stub
				return Constantor.PRODUCT_STATUS.get(arg0);
			}

			@Override
			public String getIdValue(Object arg0, int arg1) {
				// TODO Auto-generated method stub
				return arg0.toString();
			}
		};
		DropDownChoice choice=new DropDownChoice("wk_status",new PropertyModel<>(product, "status"), status,choiceRender);
		form.add(choice);
		
		
		Options ops=new Options();
		ops.set("messages", "{bold:'加粗',fontNameInherit:'字体',fontSizeInherit:'大小'}");
		
		final Editor<String> editor=new Editor<>("wk_instruction",new PropertyModel<String>(product, "instruction"),ops);
		form.add(editor);
		
		form.add(new SaveButton("wk_save",mainPanel) {
			
			@Override
			protected Map<String, Object> getInputData() {
				// TODO Auto-generated method stub
				Map<String,Object> map=new HashMap<String, Object>();
				
				map.put("product", product);
				
				return map;
			}
			
			@Override
			protected ValidatorFeedbackPanel getFeedback() {
				// TODO Auto-generated method stub
				return feedback;
			}
		});
	}

}
